<?php
include './include/common.inc.php';
include '../admin/include/group.class.php';
$group = new group;
$action = isset($action) ? $action : 'manage';
$forward = get_forward('/admin/group.php');
$view_list = get_user_resource_list('group');
switch ($action)
{
	case 'add':
		$have_perm = check_perms('G002');
		if(isset($_POST['g_name']))
		{
			if($have_perm === false)
			{
				$response->alert('您没有权限进行此操作！');
				$response->execute();
			}
			if(empty($g_name) || strlen($g_name) > 100)
			{
				$response->text_alert('error','g_name','组名称不能为空且长度不能大于100个字符');
			}
			$response->real_execute();
			$params = array(
				'g_name'	=>	$g_name,
				'description'	=>	$description
			);
			$res = $group->add_group($params);
			if(!empty($res))
			{
				$response->alert('添加成功');
				$response->redirect(urldecode($forward));
			}
			else 
			{
				$response->alert('添加失败');
				$response->script('self.location=self.location');
			}
			$response->execute();
		}
		else 
		{
			if($have_perm === false)
			{
				showmessage('您没有权限进行此操作！');
			}
			include template('admin', 'group_add');
		}
		break;
	case 'users':
		require_once('group_users.php');
		break;
	case 'roles':
		if(!class_exists('group_role'))
		{
			include '../admin/include/group_extends.class.php';
		}
		if(!class_exists('perm'))
		{
			include '../admin/include/perm.class.php';
		}
		$view_list[$gid] = isset($view_list[$gid]) ? $view_list[$gid] : '';
		if(!isset($gid) || check_perms('G009',$view_list[$gid])===false)
		{
			if(isset($edufepm) && $edufepm=='iframe')
			{
				$response->alert('您没有权限进行此操作！');
				$response->redirect($base_dir);
				$response->execute();
			}
			else 
			{
				showmessage('您没有权限进行此操作！');
			}
		}
		$perm_obj = new perm();
		if(isset($_POST['gid']))
		{
			$group_role = new group_role();
			if(is_array($r_flag) && sizeof($r_flag) > 0)
			{
				foreach ($r_flag as $k=>$r)
				{
					$group_role->delete_group_role($gid,$k);
					if($r==1)
					{
						if(isset($perm[$k]) && is_array($perm[$k]) && sizeof($perm[$k]) > 0)
						{
							$perm_list = $perm_obj->get_exist_perm($perm[$k]);
							if(sizeof($perm_list) > 0)
							{
								$group_role->add_group_role($gid,$k,$perm_list);
							}
						}
					}
				}
			}
			$response->alert('修改成功！');
			$response->script('self.location=self.location');
			$response->execute();
		}
		else 
		{
			if(!class_exists('perm'))
			{
				include '../admin/include/perm.class.php';
			}
			$perm = new perm();
			$permlist = $perm->get_all_perms();
			if(!class_exists('role'))
			{
				include '../admin/include/role.class.php';
			}
			$role = new role();
			$rolelist = $role->get_all_role_list();
			$info = $group->get_group_by_id($gid);
			$group_role = new group_role();
			$group_role_list = $group_role->get_group_roles($gid);
			include template('admin', 'group_roles');
		}
		break;
	case 'resource':
		$view_list[$gid] = isset($view_list[$gid]) ? $view_list[$gid] : '';
		if(!isset($gid) || check_perms('G008',$view_list[$gid])===false)
		{
			if(isset($edufepm) && $edufepm=='iframe')
			{
				$response->alert('您没有权限进行此操作！');
				$response->redirect($base_dir);
				$response->execute();
			}
			else 
			{
				showmessage('您没有权限进行此操作！');
			}
		}
		include '../admin/include/resource.class.php';
		$resource = new resource();
		if(isset($_POST['gid']))
		{
			if(!class_exists('group_view'))
			{
				include '../admin/include/group_extends.class.php';
			}
			if(!isset($view_type))
			{
				$response->alert('请指定资源类型！');
				$response->execute();
			}
			$group_view = new group_view();
			$info = $resource->get_res_by_view_type($view_type);
			if(!isset($info['view_type']))
			{
				$response->alert('没有您指定的资源类型！');
				$response->execute();
			}
			$res = $group_view->delete_group_view_type($gid,$view_type);
			if($res === false)
			{
				$response->alert('设置失败！');
				$response->execute();
			}
			if(isset($views) && is_array($views))
			{
				foreach ($views as $v)
				{
					$group_view->add_view($view_type,$v,$gid);
				}
			}
			
			$group_view->same_menu($gid,$view_type);
			$cache = get_cache_obj();
			$cache->del('group_view_' . $gid . '_' . $view_type);
			$group_view->edit_group_view($gid);
			$response->alert('设置成功');
			$response->script('self.location=self.location');
			$response->execute();
		}
		else 
		{
			$list = $resource->get_res();
			$info = $group->get_group_by_id($gid);
			if(!isset($view_type))
			{
				$view_type = isset($list[0]['view_type']) ? $list[0]['view_type'] : '';
			}
			$views = $group->formate_views_list($view_type,$info['view_content']);
			if(!empty($view_type))
			{
				
				$res_list = $resource->get_resource($view_type,$views);
			}
			else 
			{
				$res_list = '';
			}
			include template('admin', 'group_view');
		}
		break;
	case 'edit':
		$view_list[$gid] = isset($view_list[$gid]) ? $view_list[$gid] : '';
		if(!isset($gid) || check_perms('G003',$view_list[$gid])===false)
		{
			if(isset($edufepm) && $edufepm=='iframe')
			{
				$response->alert('您没有权限进行此操作！');
				$response->redirect($base_dir);
				$response->execute();
			}
			else 
			{
				showmessage('您没有权限进行此操作！');
			}
		}
		if(isset($_POST['g_name']))
		{
			if(empty($g_name) || strlen($g_name) > 100)
			{
				$response->text_alert('error','g_name','组名称不能为空且长度不能大于100个字符');
			}
			$response->real_execute();
			if($gid <= 0)
			{
				$response->alert('没有您要修改的组！');
				$response->execute();
			}
			$params = array(
				'g_name'	=>	$g_name,
				'description'	=>	$description
			);
			$res = $group->edit_group($params,$gid);
			if(intval($res) > 0)
			{
				$response->alert('修改成功');
				$response->redirect(urldecode($forward));
			}
			else 
			{
				$response->alert('修改失败');
			}
			$response->execute();
		}
		else 
		{
			$info = $group->get_group_by_id($gid);
			if(!isset($info['g_id']))
			{
				showmessage('要编辑的组不存在');
			}
			include template('admin', 'group_edit');
		}
		break;
	case 'del':
		$info = $group->get_group_by_id($gid);
		if(!isset($info['g_id']))
		{
			$response->alert('要删除的组不存在');
			$response->execute();
		}
		$view_list[$gid] = isset($view_list[$gid]) ? $view_list[$gid] : '';
		if(check_perms('G011',$view_list[$gid])===false)
		{
			showmessage('您没有权限进行此操作！');
		}
		$res = $group->delete_group($gid);
		if($res == false)
		{
			$response->alert('删除失败');
		}
		else 
		{
			$response->alert('删除成功');
			$response->script('self.location=self.location');
		}
		$response->execute();
		break;
	case 'delall':
		if(isset($_POST['ck']))
		{
			foreach ($ck as $gid)
			{
				$view_list[$gid] = isset($view_list[$gid]) ? $view_list[$gid] : '';
				if(check_perms('G011',$view_list[$gid])===false)
				{
					continue;
				}
				$group->delete_group($gid);
			}
			$response->alert('删除成功！');
			$response->script('self.location=self.location');
			$response->execute();
		}
		else 
		{
			$response->alert('请选择要删除的组');
			$response->execute();
		}
		break;
	case 'listorder':
		if(check_perms('G010')===false)
		{
			$response->alert('您没有权限进行此操作！');
			$response->execute();
		}
		if(isset($_POST['listorder']))
		{
			foreach ($listorder as $gid=>$listorder)
			{
				if(!isset($gid) || check_perms('G010',$view_list[$gid])===false)
				{
					continue;
				}
				$group->edit_group(array('listorder'=>$listorder),$gid);
			}
			$response->alert('排序成功！');
			$response->script('self.location=self.location');
			$response->execute();
		}
		break;
	case 'search':
		$url = $self_url;
		$url .= isset($gname) && !empty($gname) ? '?gname='.urlencode($gname) : '';
		$response->redirect($url);
		$response->execute();
		break;
	case 'manage':
	default:
		if(check_perms('G001')===false)
		{
			showmessage('您没有权限进行此操作！');
		}
		$page = isset($page) ? $page : 1;
		$page = max(1,intval($page));
		$pagesize = 20;
		$gname = isset($gname) ? $gname : '';
		$canview = ($current_user['usertype']==USER_TYPE_SUPER) ? true : get_view_resource('group');
		$list = $group->search_group($page,$pagesize,$canview,$gname,'g_id,g_name,listorder,inputtime');
		$pages = $group->pages;
		$forward = make_forward();
		include template('admin', 'group_manage');
}
?>